const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');
const dateTimePicker = require('../../utils/dateTimePicker.js');
var app = getApp();
Page({
  data: {
    startTime: null,
    endTime: null,
    userId: null,
    dateTimeArray1: null,
    dateTimeArray: null,
    startYear: 2020,
    endYear: 2050,
    hasLogin: false,
    deptName: '',
    username:'',
    dutyList:[],
    page: 1,
    limit: 10,
    count: 0,
    showPage: false,
    scrollTop: 0
  },

onLoad(){
    // 获取完整的年月日 时分秒，以及默认显示的数组
   var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
   var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();

    this.setData({
        dateTimeArray: obj1.dateTimeArray,
        dateTimeArray1: obj1.dateTimeArray,
        startTime: obj.dateTime,
        endTime: obj1.dateTime
    });
},


  onReady: function() {

  },
  onShow: function() {
    // 页面显示
    this.setData({
      hasLogin: app.globalData.hasLogin
    });

  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭

  },

  goLogin() {
    wx.navigateTo({
      url: "/pages/auth/login/login"
    });
  },    
changeStartDate(e) {
  this.setData({ startTime: e.detail.value });
},

changeEndDate(e) {
  this.setData({ endTime: e.detail.value });
},

changeStartDateColumn(e) {
    var arr = this.data.startTime, dateArr = this.data.dateTimeArray;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    try {
      this.setData({
        dateTimeArray: dateArr
      });
    } catch (err) {
     console.log(err);
    }

},

changeEndDateColumn(e) {
  var arr = this.data.endTime, dateArr = this.data.dateTimeArray1;

  arr[e.detail.column] = e.detail.value;
  dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

  try {
    this.setData({
      dateTimeArray1: dateArr
    });
  } catch (err) {
     console.log(err);
  }

  },
  clearInput: function(e) {
    switch (e.currentTarget.id) {
      case 'clear-username':
        this.setData({
          username: ''
        });
        break;
      case 'clear-deptName':
        this.setData({
          deptName: ''
        });
        break;
    }
  },
  dutyQuery: function() {
    var that = this;
    that.setData({
      scrollTop: 0,
      showPage: false,
      dutyList: []
    });

    util.request(api.dutyQuery, {
      page: that.data.page,
      limit: that.data.limit,
      startTime: this.data.dateTimeArray[0][this.data.startTime[0]]+"-"+this.data.dateTimeArray[1][this.data.startTime[1]]+"-"+this.data.dateTimeArray[2][this.data.startTime[2]]+" "+this.data.dateTimeArray[3][this.data.startTime[3]]+":"+this.data.dateTimeArray[4][this.data.startTime[4]]+":00",
      endTime: this.data.dateTimeArray1[0][this.data.endTime[0]]+"-"+this.data.dateTimeArray1[1][this.data.endTime[1]]+"-"+this.data.dateTimeArray1[2][this.data.endTime[2]]+" "+this.data.dateTimeArray1[3][this.data.endTime[3]]+":"+this.data.dateTimeArray1[4][this.data.endTime[4]]+":00",
      userId:wx.getStorageSync('userId'),
      userName:this.data.username,
      deptName:this.data.deptName
    }).then(function(res) {
      if (res.errno === 0) {
        if(res.data.total==0 ||res.data.list==[]){
          wx.showToast({
            title: '无任何值班信息',
            icon: 'success',
            duration: 2000
          });
        }else{
          that.setData({
            scrollTop: 0,
            dutyList: res.data.list,
            showPage: true,
            count: res.data.total
          });
          wx.hideToast();
        }
      }
    
    });
  },

  bindUsernameInput: function(e) {

    this.setData({
      username: e.detail.value
    });
  },
  bindDeptNameInput: function(e) {

    this.setData({
      deptName: e.detail.value
    });
  },

  bindEndTimeInput: function(e) {

    this.setData({
      endTime: e.detail.value
    });
  },
  clearInput: function(e) {
    switch (e.currentTarget.id) {
      case 'clear-username':
        this.setData({
          username: ''
        });
        break;
      case 'clear-password':
        this.setData({
          password: ''
        });
        break;
      case 'clear-code':
        this.setData({
          code: ''
        });
        break;
    }
  },
nextPage: function() {
    var that = this;
    if (this.data.page > that.data.count / that.data.limit) {
      return true;
    }


    that.setData({
      page: that.data.page + 1
    });

    this.dutyQuery();

  },
  prevPage: function() {
    if (this.data.page <= 1) {
      return false;
    }

    var that = this;
    that.setData({
      page: that.data.page - 1
    });
    this.dutyQuery();
  }
})